package com.itheima.web.controller.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.service.sms.NewProductService;
import com.itheima.utils.PageResult;
import com.itheima.utils.PageUtils;
import com.itheima.utils.ResultData;
import com.itheima.vo.sms.NewProduct;
import com.itheima.vo.sms.NewProductVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/home/newProduct")
public class NewProductController {
    @DubboReference
    private NewProductService newProductService;

    /**
     * 添加首页新品
     * @param newProducts
     * @return
     */
    @PostMapping("/create")
    public ResultData create(@RequestBody List<NewProduct> newProducts){
        newProductService.saveBatch(newProducts);
        return ResultData.ok("添加成功",null);
    }

    /**
     * 批量删除首页新品
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ResultData delete(@RequestParam List ids){
        newProductService.removeByIds(ids);
        return ResultData.ok("删除成功",null);
    }

    /**
     * 分页查询首页新品
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus){
        Page<NewProduct> pageInfo = newProductService.getPage(pageNum, pageSize, productName, recommendStatus);
        PageResult page = PageResult.getPage(pageInfo);
        return ResultData.ok("查询成功",page);
    }

    /**
     * 批量修改首页新品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public ResultData updateRecommendStatusByIds(Integer[] ids, Integer recommendStatus){
        newProductService.updateByStatus(Arrays.asList(ids),recommendStatus);
        return ResultData.ok("修改成功",null);
    }

    /**
     * 修改首页新品排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("update/sort/{id}")
    public ResultData updateSortById(Integer id, Integer sort){
        NewProduct newProduct=newProductService.getById(id);
        newProduct.setSort(sort);
        newProductService.updateById(newProduct);
        return ResultData.ok("修改成功",null);
    }
}
